package com.jxpanda.spring.module.auth.core.register.strategy;

import com.jxpanda.spring.module.auth.core.OAuth2StrategyResolver;
import com.jxpanda.spring.module.auth.core.user.OAuth2UserContext;
import org.springframework.security.core.userdetails.UserDetails;
import reactor.core.publisher.Mono;

public class OAuth2ReactiveRegisterStrategyResolver extends OAuth2StrategyResolver<OAuth2ReactiveRegisterStrategy> {

    public <U extends UserDetails> Mono<U> resolve(OAuth2UserContext oAuth2UserContext) {
        return selectStrategy(oAuth2UserContext.getClientId(), oAuth2UserContext.getGrantType())
                .flatMap(strategy -> {
                    if (strategy.shouldAutoRegister()) {
                        return strategy.register(oAuth2UserContext);
                    }
                    return strategy.fallback(oAuth2UserContext);
                });
    }

    public static OAuth2ReactiveRegisterStrategyResolver emptyResolver() {
        OAuth2ReactiveRegisterStrategyResolver resolver = new OAuth2ReactiveRegisterStrategyResolver();
        resolver.putStrategy("", grantType -> true);
        return resolver;
    }


}
